import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttercalcpokers/utils/style_config.dart';

// ignore: must_be_immutable
class UserList extends StatefulWidget {
  List<Map> userList = [];
  int userSelectIndex;
  int userPokerSelectIndex;
  var selectUserItem;
  var selectUserItemPoker;
  bool isSelectPublic;

  UserList(
      {Key key,
      this.userList,
      this.userSelectIndex,
      this.userPokerSelectIndex,
      this.selectUserItem,
      this.selectUserItemPoker,
      this.isSelectPublic})
      : super(key: key);

  @override
  _UserListState createState() => _UserListState();
}

class _UserListState extends State<UserList> {
  Widget userBuilder() {
    int len = widget.userList.length;
    int userSelectIndex = widget.userSelectIndex;
    int userPokerSelectIndex = widget.userPokerSelectIndex;
    List<Widget> _widgets = [];
    Widget content;

    BoxDecoration _select = BoxDecoration(
      border: Border.all(color: Colors.redAccent, width: 2.0),
      borderRadius: BorderRadius.all(
        Radius.circular(5.0),
      ),
    );
    BoxDecoration _notSelect = BoxDecoration(
      border: Border.all(color: Color.fromRGBO(255, 255, 255, 0), width: 2.0),
      borderRadius: BorderRadius.all(
        Radius.circular(5.0),
      ),
    );
    Widget _notData = Container(
      width: ScreenUtil().setWidth(130),
      height: ScreenUtil().setHeight(220),
      decoration: BoxDecoration(
        color: Color.fromRGBO(255, 255, 255, 1),
        borderRadius: BorderRadius.all(
          Radius.circular(5.0),
        ),
      ),
    );
    Widget dataBuilder(src, i) {
      return Container(
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: Color.fromRGBO(255, 255, 255, 1),
          borderRadius: BorderRadius.all(
            Radius.circular(4.0),
          ),
        ),
        child: Text(
          src,
          softWrap: true,
          textAlign: TextAlign.left,
          overflow: TextOverflow.ellipsis,
          maxLines: 2,
          style: TextStyle(
              color: Colors.black,
              fontSize: i <= 5 ? fontSize_20 : fontSize_16),
        ),
      );
    }

    if (len <= 5) {
      for (int i = 0; i < len; i++) {
        Widget items;
        Map item = widget.userList[i];
        if (len <= 2) {
          Widget _poker = Row(
            children: <Widget>[
              Container(
                margin: EdgeInsets.only(right: ScreenUtil().setWidth(20)),
                child: Text(
                  (i + 1).toString(),
                  style: TextStyle(color: color_f9f5cf, fontSize: 8.0),
                ),
              ),
              InkWell(
                onTap: () {
                  widget.selectUserItem(i);
                  widget.selectUserItemPoker(0);
                },
                child: Container(
                  width: ScreenUtil().setWidth(130),
                  height: ScreenUtil().setHeight(200),
                  margin: EdgeInsets.only(right: 10.0),
                  decoration: widget.isSelectPublic == false &&
                          userSelectIndex == i &&
                          userPokerSelectIndex == 0
                      ? _select
                      : _notSelect,
                  alignment: Alignment.center,
                  child: item["firstSrc"].isNotEmpty
                      ? dataBuilder(item["firstSrc"], i)
                      : _notData,
                ),
              ),
              InkWell(
                onTap: () {
                  widget.selectUserItem(i);
                  widget.selectUserItemPoker(1);
                },
                child: Container(
                  width: ScreenUtil().setWidth(130),
                  height: ScreenUtil().setHeight(200),
                  margin: EdgeInsets.only(right: 10.0),
                  decoration: widget.isSelectPublic == false &&
                          userSelectIndex == i &&
                          userPokerSelectIndex == 1
                      ? _select
                      : _notSelect,
                  alignment: Alignment.center,
                  child: item["secondSrc"].isNotEmpty
                      ? dataBuilder(item["secondSrc"], i)
                      : _notData,
                ),
              ),
              Container(
                alignment: Alignment.center,
                padding: EdgeInsets.only(
                  top: 15.0,
                ),
                child: Column(
                  children: <Widget>[
                    Center(
                      child: Text(
                        item["winCal"].toString() + "%",
                        style: TextStyle(
                            fontSize: fontSize_36, color: color_f9f5cf),
                      ),
                    ),
                    Center(
                      child: Text(
                        "平局" + item["drawCal"].toString() + "%",
                        style: TextStyle(
                            fontSize: fontSize_36, color: color_f9f5cf),
                      ),
                    )
                  ],
                ),
              )
            ],
          );

          Widget _ratePoker = Row(
            children: <Widget>[
              Container(
                margin: EdgeInsets.only(right: ScreenUtil().setWidth(20)),
                child: Text(
                  (i + 1).toString(),
                  style: TextStyle(color: Colors.white, fontSize: 8.0),
                ),
              ),
              InkWell(
                onTap: () {
                  widget.selectUserItem(i);
                  widget.selectUserItemPoker(1);
                },
                child: Container(
                  width: ScreenUtil().setWidth(260),
                  height: ScreenUtil().setHeight(200),
                  padding: EdgeInsets.all(5.0),
                  margin: EdgeInsets.only(right: 10.0),
                  decoration: widget.isSelectPublic == false &&
                          userSelectIndex == i &&
                          userPokerSelectIndex == 1
                      ? _select
                      : _notSelect,
                  alignment: Alignment.center,
                  child: Center(
                    child: dataBuilder(item["RateValue"], i),
                  ),
                ),
              ),
              Container(
                alignment: Alignment.center,
                padding: EdgeInsets.only(
                  top: 15.0,
                ),
                child: Column(
                  children: <Widget>[
                    Center(
                      child: Text(
                        item["winCal"].toString() + "%",
                        style: TextStyle(
                            fontSize: fontSize_36, color: color_f9f5cf),
                      ),
                    ),
                    Center(
                      child: Text(
                        "平局" + item["drawCal"].toString() + "%",
                        style: TextStyle(
                            fontSize: fontSize_36, color: color_f9f5cf),
                      ),
                    )
                  ],
                ),
              )
            ],
          );

          items = Container(
//            width: ScreenUtil().setWidth(600),
            height: ScreenUtil().setHeight(220),
            padding: EdgeInsets.only(
                left: ScreenUtil().setWidth(20),
                right: ScreenUtil().setWidth(20)),
            alignment: Alignment.center,
            child: Material(
              color: Color.fromRGBO(255, 255, 255, 0),
              child: item["isRate"] == true ? _ratePoker : _poker,
            ),
          );
        }
        if (len > 2 && len <= 5) {
          Widget _poker = Row(
            children: <Widget>[
              Text(
                (i + 1).toString(),
                style: TextStyle(color: Colors.white, fontSize: 8.0),
              ),
              InkWell(
                onTap: () {
                  widget.selectUserItem(i);
                  widget.selectUserItemPoker(0);
                },
                child: Container(
                  width: ScreenUtil().setWidth(150 - 10 * len),
                  height: ScreenUtil().setHeight(520 / len),
                  margin: EdgeInsets.only(right: 10.0),
                  decoration: widget.isSelectPublic == false &&
                          userSelectIndex == i &&
                          userPokerSelectIndex == 0
                      ? _select
                      : _notSelect,
                  alignment: Alignment.center,
                  child: item["firstSrc"].isNotEmpty
                      ? dataBuilder(item["firstSrc"], i)
                      : _notData,
                ),
              ),
              InkWell(
                onTap: () {
                  widget.selectUserItem(i);
                  widget.selectUserItemPoker(1);
                },
                child: Container(
                  width: ScreenUtil().setWidth(150 - 10 * len),
                  height: ScreenUtil().setHeight(520 / len),
                  margin: EdgeInsets.only(right: 10.0),
                  decoration: widget.isSelectPublic == false &&
                          userSelectIndex == i &&
                          userPokerSelectIndex == 1
                      ? _select
                      : _notSelect,
                  alignment: Alignment.center,
                  child: item["secondSrc"].isNotEmpty
                      ? dataBuilder(item["secondSrc"], i)
                      : _notData,
                ),
              ),
              Container(
                alignment: Alignment.center,
                padding: EdgeInsets.only(
                  top: 15.0,
                ),
                child: Column(
                  children: <Widget>[
                    Center(
                      child: Text(
                        item["winCal"].toString() + "%",
                        style: TextStyle(
                            fontSize: fontSize_24, color: color_f9f5cf),
                      ),
                    ),
                    Center(
                      child: Text(
                        "平局" + item["drawCal"].toString() + "%",
                        style: TextStyle(
                            fontSize: fontSize_24, color: color_f9f5cf),
                      ),
                    )
                  ],
                ),
              )
            ],
          );

          Widget _ratePoker = Row(
            children: <Widget>[
              Container(
                margin: EdgeInsets.only(right: ScreenUtil().setWidth(20)),
                child: Text(
                  (i + 1).toString(),
                  style: TextStyle(color: Colors.white, fontSize: 8.0),
                ),
              ),
              InkWell(
                onTap: () {
                  widget.selectUserItem(i);
                  widget.selectUserItemPoker(1);
                },
                child: Container(
                  width: ScreenUtil().setWidth(300 - 10 * len),
                  height: ScreenUtil().setHeight(520 / len),
                  padding: EdgeInsets.all(5.0),
                  margin: EdgeInsets.only(right: 10.0),
                  decoration: widget.isSelectPublic == false &&
                          userSelectIndex == i &&
                          userPokerSelectIndex == 1
                      ? _select
                      : _notSelect,
                  alignment: Alignment.center,
                  child: Center(
                    child: dataBuilder(item["RateValue"], i),
                  ),
                ),
              ),
              Container(
                alignment: Alignment.center,
                padding: EdgeInsets.only(
                  top: 15.0,
                ),
                child: Column(
                  children: <Widget>[
                    Center(
                      child: Text(
                        item["winCal"].toString() + "%",
                        style: TextStyle(fontSize: 8.0, color: color_f9f5cf),
                      ),
                    ),
                    Center(
                      child: Text(
                        "平局" + item["drawCal"].toString() + "%",
                        style: TextStyle(fontSize: 8.0, color: color_f9f5cf),
                      ),
                    )
                  ],
                ),
              )
            ],
          );
          items = Container(
            width: ScreenUtil().setWidth(600 - 20 * len),
            height: ScreenUtil().setHeight(600 / len),
            padding: EdgeInsets.only(left: 10.0, right: 10.0),
            alignment: Alignment.center,
            child: Material(
              color: Color.fromRGBO(255, 255, 255, 0),
              child: item["isRate"] == true ? _ratePoker : _poker,
            ),
          );
        }
        _widgets.add(items);
      }
      if (len <= 5) {
        content = Column(
          children: _widgets,
        );
      }
    }
    if (len > 5) {
      Widget _notRateBuild(i) {
        return Row(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(right: ScreenUtil().setWidth(10)),
              child: Text(
                i == 9 ? "T" : (i + 1).toString(),
                style: TextStyle(color: Colors.white, fontSize: 8.0),
              ),
            ),
            InkWell(
              onTap: () {
                widget.selectUserItem(i);
                widget.selectUserItemPoker(0);
              },
              child: Container(
                width: ScreenUtil().setWidth(60),
                height: ScreenUtil().setHeight(80),
                margin: EdgeInsets.only(right: 5.0),
                decoration: widget.isSelectPublic == false &&
                        userSelectIndex == i &&
                        userPokerSelectIndex == 0
                    ? _select
                    : _notSelect,
                alignment: Alignment.center,
                child: widget.userList[i]["firstSrc"].isNotEmpty
                    ? dataBuilder(widget.userList[i]["firstSrc"], i)
                    : _notData,
              ),
            ),
            InkWell(
              onTap: () {
                widget.selectUserItem(i);
                widget.selectUserItemPoker(1);
              },
              child: Container(
                width: ScreenUtil().setWidth(60),
                height: ScreenUtil().setHeight(80),
                margin: EdgeInsets.only(right: 5.0),
                decoration: widget.isSelectPublic == false &&
                        userSelectIndex == i &&
                        userPokerSelectIndex == 1
                    ? _select
                    : _notSelect,
                alignment: Alignment.center,
                child: widget.userList[i]["secondSrc"].isNotEmpty
                    ? dataBuilder(widget.userList[i]["secondSrc"], i)
                    : _notData,
              ),
            ),
            Container(
              alignment: Alignment.center,
              padding: EdgeInsets.only(top: 15.0),
              child: Column(
                children: <Widget>[
                  Center(
                    child: Text(widget.userList[i]["winCal"].toString() + "%",
                        style: TextStyle(
                            fontSize: fontSize_16, color: color_f9f5cf)),
                  ),
                  Center(
                    child: Text(
                        "平局" + widget.userList[i]["drawCal"].toString() + "%",
                        style: TextStyle(
                            fontSize: fontSize_16, color: color_f9f5cf)),
                  )
                ],
              ),
            )
          ],
        );
      }

      Widget _isRateBuilder(i) {
        return Row(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(right: ScreenUtil().setWidth(10)),
              child: Text(
                (i + 1).toString(),
                style: TextStyle(color: Colors.white, fontSize: 8.0),
              ),
            ),
            InkWell(
              onTap: () {
                widget.selectUserItem(i);
                widget.selectUserItemPoker(1);
              },
              child: Container(
                width: ScreenUtil().setWidth(130),
                height: ScreenUtil().setHeight(70),
                padding: EdgeInsets.all(5.0),
                margin: EdgeInsets.only(right: 10.0),
                decoration: widget.isSelectPublic == false &&
                        userSelectIndex == i &&
                        userPokerSelectIndex == 1
                    ? _select
                    : _notSelect,
                alignment: Alignment.center,
                child: Center(
                  child: dataBuilder(widget.userList[i]["RateValue"], i),
                ),
              ),
            ),
            Container(
              alignment: Alignment.center,
              padding: EdgeInsets.only(
                top: 15.0,
              ),
              child: Column(
                children: <Widget>[
                  Center(
                    child: Text(
                      widget.userList[i]["winCal"].toString() + "%",
                      style: TextStyle(fontSize: 8.0, color: color_f9f5cf),
                    ),
                  ),
                  Center(
                    child: Text(
                      "平局" + widget.userList[i]["drawCal"].toString() + "%",
                      style: TextStyle(fontSize: 8.0, color: color_f9f5cf),
                    ),
                  )
                ],
              ),
            )
          ],
        );
      }

      content = GridView.builder(
        shrinkWrap: true,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          mainAxisSpacing: 4.0,
          crossAxisSpacing: 4.0,
          childAspectRatio: 3.0,
        ),
        itemCount: len,
        itemBuilder: (context, i) {
          return Container(
            width: ScreenUtil().setWidth(280),
            height: ScreenUtil().setHeight(100),
            padding: EdgeInsets.only(left: 10.0, right: 10.0),
            alignment: Alignment.center,
            child: Material(
              color: Color.fromRGBO(255, 255, 255, 0),
              child: widget.userList[i]["isRate"] == true
                  ? _isRateBuilder(i)
                  : _notRateBuild(i),
            ),
          );
        },
      );
    }
    return content;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: ScreenUtil().setWidth(600),
      height: ScreenUtil().setHeight(600),
      child: userBuilder(),
    );
  }
}
